package cn.pro.utils;


import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;

@Slf4j
public class ObjectUtils {
    /**
     * 判断对象是否完全为空
     *
     * @param object
     * @return
     */
    public static boolean objectCheckIsNull(Object object) {
        boolean flag = true; //定义返回结果，默认为true

        if (Objects.isNull(object)) {
            flag = true;
        } else {
            Class clazz = (Class) object.getClass(); // 得到类对象
            Field fields[] = clazz.getDeclaredFields(); // 得到所有属性
            for (Field field : fields) {
                field.setAccessible(true);
                Object fieldValue = null;
                try {
                    fieldValue = field.get(object); //得到属性值
                    Type fieldType = field.getGenericType();//得到属性类型
                    String fieldName = field.getName(); // 得到属性名
                    log.info("属性类型：" + fieldType + ",属性名：" + fieldName + ",属性值：" + fieldValue);
                } catch (IllegalArgumentException e) {
                    log.error(e.getMessage(), e);
                } catch (IllegalAccessException e) {
                    log.error(e.getMessage(), e);
                }
                if (fieldValue != null) {  //只要有一个属性值不为null 就返回false 表示对象不为null
                    flag = false;
                    break;
                }
            }
        }

        return flag;
    }

    public static String couponCodeOne(){
        int first = new Random().nextInt(9);
        int rnd = UUID.randomUUID().toString().hashCode();
        if(rnd < 0){
                rnd = -rnd;
        }
        return first+String.format("%010d", rnd);
    }

    public static String couponCodeTwo(String couponType) {
        int first = new Random().nextInt(9);
        int rnd = UUID.randomUUID().toString().hashCode();
        if (rnd < 0) {
            rnd = -rnd;
        }
        return couponType + first + String.format("%010d", rnd);
    }
}
